package com.shu.controller;

import com.shu.domain.po.ResultBean;
import com.shu.domain.po.User;
import com.shu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by shu on 2017/8/14.
 */
@RestController
@RequestMapping("/demo")
public class DemoController {

    private final static Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Resource
    private UserService userService;

//    @Resource
//    private Producer producer;

//    @Value("${spring.datasource.master.username}")
//    private String dd;

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public Object addUser() throws Exception {
        User user = new User();
        user.setId(UUID.randomUUID().toString().replaceAll("-",""));
        user.setUsername("name");
        user.setPassword("123123");
        user.setRole("admin");
        //userService.addUser(user);

        ResultBean<User> resultBean = new ResultBean<>();

        resultBean.setData(user);

        return resultBean;
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public Object getUsers() throws Exception{

      Subject currentUser = SecurityUtils.getSubject();

      Object userId = currentUser.getPrincipal();

      return userService.selectByPage();
    }

    @RequestMapping(value = "/message",method = RequestMethod.POST)
    public ResultBean sendMessage() throws Exception{
        //producer.send("发送一个消息");

        ResultBean<Map<String,Object>> resultBean = new ResultBean<>();

        Map<String,Object> map = new HashMap<>();

        map.put("name","message");

        resultBean.setData(map);

        return resultBean;


    }

    @RequestMapping(value = "/log",method = RequestMethod.GET)
    public void log() throws Exception{
        logger.info("info");
        logger.debug("debug");
        logger.error("error");
        logger.warn("warn");
        //throw new CustomException("抛出异常",500);
    }

}
